package com.stylefeng.guns.modular.hhtb.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.modular.hhtb.model.Hhtbusers;
import com.stylefeng.guns.modular.hhtb.model.Inviteid;
import com.stylefeng.guns.modular.hhtb.service.IInviteidService;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.hhtb.service.IHhtbusersService;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-05 15:13:37
 */
@Controller
@RequestMapping("/hhtbusers")
public class HhtbusersController extends BaseController {

    private String PREFIX = "/hhtb/hhtbusers/";

    @Autowired
    private IHhtbusersService hhtbusersService;

    @Autowired
    private IInviteidService inviteidService;

    /**
     * 跳转到用户管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "hhtbusers.html";
    }

    /**
     * 跳转到添加用户管理
     */
    @RequestMapping("/hhtbusers_add")
    public String hhtbusersAdd() {
        return PREFIX + "hhtbusers_add.html";
    }

    /**
     * 跳转到修改用户管理
     */
    @RequestMapping("/hhtbusers_update/{hhtbusersId}")
    public String hhtbusersUpdate(@PathVariable Integer hhtbusersId, Model model) {
        Hhtbusers hhtbusers = hhtbusersService.selectById(hhtbusersId);
        model.addAttribute("item",hhtbusers);
        LogObjectHolder.me().set(hhtbusers);
        return PREFIX + "hhtbusers_edit.html";
    }

    /**
     * 获取用户管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Hhtbusers hhtbusers) {
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        List<Map<String, Object>> hhtbusersList = hhtbusersService.selectAll(page, hhtbusers);
        page.setRecords(hhtbusersList);
        return super.packForBT(page);
    }

    /**
     * 新增用户管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Hhtbusers hhtbusers) {
        hhtbusersService.insert(hhtbusers);
        return SUCCESS_TIP;
    }

    /**
     * 删除用户管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer hhtbusersId) {
        hhtbusersService.deleteById(hhtbusersId);
        return SUCCESS_TIP;
    }

    /**
     * 修改用户管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Hhtbusers hhtbusers) {
        hhtbusersService.updateById(hhtbusers);
        return SUCCESS_TIP;
    }

    /**
     * 用户管理详情
     */
    @RequestMapping(value = "/detail/{hhtbusersId}")
    @ResponseBody
    public Object detail(@PathVariable("hhtbusersId") Integer hhtbusersId) {
        return hhtbusersService.selectById(hhtbusersId);
    }

    /**
     * 跳转到用户推荐列表
     */
    @RequestMapping("/invitedList/{userId}")
    public String invitedList(@PathVariable("userId")Integer userId, Model model) {
        model.addAttribute("userId", userId);
        LogObjectHolder.me().set(userId);
        return PREFIX + "hhtbusers_invited.html";
    }

    /**
     * 获取用户推荐列表
     */
    @RequestMapping(value = "/selectInvitedList")
    @ResponseBody
    public Object selectInvitedList(@Param("userId")Integer userId,Hhtbusers hhtbusers) {
        Hhtbusers old = hhtbusersService.selectById(userId);
        Inviteid inviteid=new Inviteid();
        inviteid.setUserPhone(old.getUserPhone());
        EntityWrapper<Inviteid> entityWrapper=new EntityWrapper<Inviteid>(inviteid);
        Inviteid inviteid1 = inviteidService.selectOne(entityWrapper);
        if (inviteid1!=null){
            hhtbusers.setUserPhone(inviteid1.getInvitePhone());
        }
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
        List<Map<String, Object>> hhtbusersList = hhtbusersService.selectAll(page, hhtbusers);
        page.setRecords(hhtbusersList);
        return super.packForBT(page);
    }

}
